<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class Room extends Base
{
    protected $table = 'room';
    protected $connection = "mysql_kkaudio";

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function getImageAttribute($value)
    {
        return build_image($value);
    }

    public function getRoomInfoToUserId($user_id)
    {
        $where['user_id'] = $user_id;
        $where['business_type'] = 1;
        $data = DB::connection("mysql_kkaudio")
            ->table("room")
            ->where($where)
            ->first();
        return self::objToArray($data);
    }

    public function getRoomInfoToRoomIds($room_ids = [])
    {
        $where['business_type'] = 1;
        $data = DB::connection("mysql_kkaudio")
            ->table("room")
            ->where($where)
            ->whereIn('room_id', $room_ids)
            ->get();
        return self::objToArray($data);
    }

    public function getRoomInfoToRoomIdCache($room_id)
    {
        $key = 'ROOM:INFO' . '-' . $room_id;
        $cache = Redis::GET($key);
        if (!$cache) {
            $where['room_id'] = $room_id;
            $data = DB::connection("mysql_kkaudio")
                ->table("room")
                ->where($where)
                ->get();
            $data = self::objToArray($data);
            Redis::SET($key, json_encode($data[0]), 'EX', 60);
        }
        return Redis::GET($key);
    }

    public function getRoomInfoToRoomIdCacheToArr($room_id)
    {
        $key = 'ROOM:INFO' . '-' . $room_id;
        $cache = Redis::GET($key);
        if (!$cache) {
            $where['room_id'] = $room_id;
            $data = DB::connection("mysql_kkaudio")
                ->table("room")
                ->where($where)
                ->get();
            $data = self::objToArray($data);
            if (!$data) {
                return [];
            }
            Redis::SET($key, json_encode($data[0]), 'EX', 60);
        }
        return json_decode(Redis::GET($key), true);
    }


    //获取用户麦上时长
    public function getUserJoinRoomTime($user_id, $timestamp)
    {

    }
}
